package com.facotry.util;

import com.facotry.entity.FacUser;
import com.facotry.ex.NoLoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * --- 代码敲烂 月薪过万 ---
 *
 * @author 崔瑜航
 * @date 2024/7/30
 * @desc  设置拦截器
 */
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisUtil redisUtil;
    // 登录成功后，将token发送给前端
    // 前端发送请求时，需要将token放到请求头中，发送给后台

    // 本例，从Authorization这个请求头中获取token值
    // 注意,需要将前端的请求头改变为Authorization
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("Authorization");
        if (token == null || "".equals(token)){
            throw new NoLoginException(50008,"无效令牌,请重新登录");
        }
        FacUser facUser = redisUtil.getCacheObject("user:" + token);
        if (facUser == null){
            throw new NoLoginException(50014,"身份信息失效,请重新登录");
        }
        return true;
    }
}
